package com.rill.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rill.paging.Page;
import com.rill.paging.Result;
import com.rill.po.Article;
import com.rill.service.ArticleService;

public class ShowAllArticleAction extends ActionSupport {
	private ArticleService articleService;
	private int currentPage;//设置当前页，在浏览器显示栏上传递参数给action，action来获得

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	@Override
	public String execute() throws Exception {
		/*// 首先要获得session
		Map session = ActionContext.getContext().getSession();
		// 获得username
		String username = (String) session.get("username");
		*/
		//设置page的信息，传递给service
		Page page = new Page();//创建page实例
		page.setEveryPage(5);
		page.setCurrentPage(this.getCurrentPage());
		
		Result result = articleService.showArticleByPage(page);//按照分页信息查询用户所有文章
		//获取page和list
		page =result.getPage();
		List <Article> all = result.getList();
		
		// 查出所有后让它在一个jsp中显示：把查询到的结果保存在一个范围，request
		HttpServletRequest request = ServletActionContext.getRequest();
		//把返回的list和page存在request域中
		request.setAttribute("all", all);
		request.setAttribute("page", page);
		return SUCCESS;
	}
}
